有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用JsonParser的java Jackson反序列化正在跳过@context的第一个键值对

我正在构建一个Jackson反序列化应用程序,它可以处理CustomerListCustomer的反序列化。用户可以提供任何输入,根据输入,代码将决定提供的输入JSON是CustomerList还是Single Customer

除了一件小事之外,一切都在按预期进行。当我提供CustomerListJSON时,它将跳过第一个key value对。在我的例子中,它跳过了@Context

以下是我正在尝试反序列化的JSON

{
  "@context": [
    "https://stackoverflow.com",
    {
      "example": "https://example.com"
    }
  ],
  "isA": "CustomerDocument",
  "customerList": [
    {
      "isA": "Customer",
      "name": "Batman",
      "age": "2008"
    }
  ]
}

以下是我的客户POJO课程:

@Data
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, visible = true, property = "isA")
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Customer implements BaseResponse {
    private String isA;
    private String name;
    private String age;
}


@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, visible = true, property = "isA")
@JsonSubTypes({
        @JsonSubTypes.Type(value = Customer.class, name = "Customer")})
interface BaseResponse {
}

主要内容如下:

public class JacksonMain {
    public static void main(String[] args) throws IOException {
        final InputStream jsonStream = JacksonMain.class.getClassLoader().getResourceAsStream("Customer.json");
        final JsonParser jsonParser = new JsonFactory().createParser(jsonStream);
        final ObjectMapper objectMapper = new ObjectMapper();
        jsonParser.setCodec(objectMapper);

        //Goto the start of the document
        jsonParser.nextToken();

        try {
            BaseResponse baseResponse = objectMapper.readValue(jsonParser, BaseResponse.class);
            System.out.println("SINGLE EVENT INPUT" + baseResponse.toString());
        } catch (Exception e) {
            System.out.println("LIST OF CUSTOMER INPUT");
            //Go until the customerList has been reached
            while (!jsonParser.getText().equals("customerList")) {
                System.out.println("Current Token Name : " + jsonParser.getCurrentName());
                if (jsonParser.getCurrentName() != null && jsonParser.getCurrentName().equalsIgnoreCase("@context")) {
                    System.out.println("WITHIN CONTEXT");
                }
                jsonParser.nextToken();
            }
            jsonParser.nextToken();

            //Loop through each object within the customerList and deserilize them
            while (jsonParser.nextToken() != JsonToken.END_ARRAY) {
                final JsonNode customerNode = jsonParser.readValueAsTree();
                final String eventType = customerNode.get("isA").asText();
                Object event = objectMapper.treeToValue(customerNode, BaseResponse.class);
                System.out.println(event.toString());
            }
        }
    }
}

运行应用程序时,我得到以下响应:

LIST OF CUSTOMER INPUT
Current Token Name : isA
Customer(isA=Customer, name=Batman, age=2008)

我们可以看到它只打印Current Token Name: isA,我希望它打印isA and @Context,因为它在isA之前

我看到的奇怪的事情是,如果我在我的JSON中切换isA@context的位置,就像这样:

{
  "isA": "CustomerDocument",
  "@context": [
    "https://stackoverflow.com",
    {
      "example": "https://example.com"
    }
  ],
  "customerList": [
    {
      "isA": "Customer",
      "name": "Batman",
      "age": "2008"
    }
  ]
}

然后我得到这样的输出,正如您现在看到的,它读取@contextisA两者

LIST OF CUSTOMER INPUT
Current Token Name : isA
Current Token Name : @context
WITHIN CONTEXT
Current Token Name : @context
WITHIN CONTEXT
Current Token Name : null
Current Token Name : null
Current Token Name : example
Current Token Name : example
Current Token Name : null
Current Token Name : @context
WITHIN CONTEXT
Customer(isA=Customer, name=Batman, age=2008)

由于它在第二种情况下工作,我认为这与我的代码无关。但是我不确定是什么原因导致它不能在firstcase中读取(第一个是@context,第二个是isA)。有人能帮我理解这个问题,并提供一些解决办法,因为我从昨天开始就试图解决这个问题?这是杰克逊的错误吗

请注意: JSON不受我的控制,因为它来自另一个应用程序。提供的JSON是我的实际应用程序JSON的副本

我可以提供给应用程序的第二个JSON是直接客户,而不是列表:(使用当前代码,这非常有效)

{
  "@context": [
    "https://stackoverflow.com",
    {
      "example": "https://example.com"
    }
  ],
  "isA": "Customer",
  "name": "Batman",
  "age": "2008"
}

有人能帮助我理解为什么Jackson在第一次提供时跳过了@context,并提供一些解决方法,因为我从昨天开始就试图解决这个问题?这是杰克逊的错误吗


共 (0) 个答案